package ch.reevolt.mosaic.fragments;

import java.io.FileNotFoundException;

import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import ch.reevolt.mosaic.MainActivity;
import ch.reevolt.mosaic.R;
import ch.reevolt.mosaic.Receiver;

public class LeftView extends Fragment implements OnClickListener {

	private static final int SELECT_PHOTO = 100;

	private Bitmap selectedBitmap;
	private View view;
	private ImageView imageView;
	private Button button;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.left_view, container, false);
		imageView = (ImageView) view.findViewById(R.id.view);
		button = (Button) view.findViewById(R.id.searchButton);
		button.setOnClickListener(this);

		return view;
	}

	public void updateImage(Bitmap bitmap) {
		imageView.setImageBitmap(bitmap);
	}

	public void selectImage() {
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		photoPickerIntent.setType("image/*");
		startActivityForResult(photoPickerIntent, SELECT_PHOTO);
	}

	public void onActivityResult(int requestCode, int resultCode,
			Intent imageReturnedIntent) {
		super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

		switch (requestCode) {
		case SELECT_PHOTO:
			if (resultCode == MainActivity.RESULT_OK) {
				try {
					selectedBitmap = decodeUri(imageReturnedIntent.getData());
					imageView.setImageBitmap(selectedBitmap);
					
					Intent i = new Intent();
					i.setAction(ch.reevolt.mosaic.Receiver.ACTION_FRAGMENT);
					i.putExtra("PATH", selectedBitmap);

					getActivity().sendBroadcast(i);
					Log.d("---------------------------------------------------------------------->",
							"envoyé");
				} catch (FileNotFoundException e) {
					Toast.makeText(getActivity().getApplicationContext(),
							"Image not found", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
			}
		}

	}

	private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

		// Decode image size
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(getActivity().getApplicationContext()
				.getContentResolver().openInputStream(selectedImage), null, o);

		// The new size we want to scale to
		final int REQUIRED_SIZE = 1000;

		// Find the correct scale value. It should be the power of 2.
		int width_tmp = o.outWidth, height_tmp = o.outHeight;
		int scale = 1;
		while (true) {
			if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
				break;
			}
			width_tmp /= 2;
			height_tmp /= 2;
			scale *= 2;
		}

		// Decode with inSampleSize
		BitmapFactory.Options o2 = new BitmapFactory.Options();
		o2.inSampleSize = scale;
		return BitmapFactory.decodeStream(getActivity().getApplicationContext()
				.getContentResolver().openInputStream(selectedImage), null, o2);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.searchButton:
			selectImage();
			break;
		default:
		}
	}

}